package com.css.dzj.dzjap.modules.lawenforcesuper.controller;



import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.executorinfo.entity.ApExecutorInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApExtractLawerEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ApExpertExtractLawerService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 监管专家抽取执法人员Controller
 * @author liyunyue
 */
@RequestMapping("/apExpertExtractLawer")
@RestController
public class ApExpertExtractLawerController {

    @Autowired
    private ApExpertExtractLawerService apExpertExtractLawerService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    /**
     * 抽取执法人员
     * @param apExtractLawerEntity
     * @return
     */
    @PostMapping("/extractLawer")
    public R extractLawer(@RequestBody ApExtractLawerEntity apExtractLawerEntity,  HttpServletRequest request) {
        String s = "";
        try {
            if(StringUtils.isEmpty(apExtractLawerEntity.getLinkId()) || StringUtils.isEmpty(apExtractLawerEntity.getTaskId())
                    || apExtractLawerEntity.getCountryRatio() == null  || apExtractLawerEntity.getProvinceRatio() == null
                    || apExtractLawerEntity.getCityRatio() == null ) {
                return R.error("必填字段为空！");
            }


            R result = apExpertExtractLawerService.extractLawer(apExtractLawerEntity, request);
            System.out.println(result);
            if("500".equals(result.get("code").toString())) {
                return R.error(result.getMsg());
            }
            s = JSON.toJSONString(result);
            sysAuditService.saveSuccessAudit("抽取成功", Constant.APEXPERTEXTRACTLAWER, Constant.PT, Constant.EXTRACTLAWER, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("抽取失败", Constant.APEXPERTEXTRACTLAWER, Constant.YZ, Constant.EXTRACTLAWER, s, e, request);
            return R.error(e.getMessage());
        }

    }


    @GetMapping("/queryLawerByTaskIdAndExpertId")
    public R queryLawerByTaskIdAndExpertId(@RequestParam(required = true) String taskId,
                                           @RequestParam(required = true) String expertId,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
       String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            ApExtractLawerEntity apExtractLawerEntity = apExpertExtractLawerService.queryByTaskIdAndExpertId(taskId, expertId);
            QueryResults<ApExecutorInfoEntity> executorInfoEntityList = apExpertExtractLawerService.queryLawerByTaskIdAndExpertId(pageRequest, taskId, expertId);
            s = JSON.toJSONString(executorInfoEntityList);
            sysAuditService.saveSuccessAudit("导出选中数据成功", Constant.APEXPERTEXTRACTLAWER, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", executorInfoEntityList).put("extractLawer", apExtractLawerEntity);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("导出选中数据失败", Constant.APEXPERTEXTRACTLAWER, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }

}
